package com.work.keep.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.datas.NewestData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class MyListViewAdapter extends BaseAdapter {
    private Context context;
    private List<NewestData.DataEntity> newestDataLists;
    private ImageLoader imageLoader;
    private VolleySingleton volleySingleton;
//    private MyPagerAdapter pagerAdapter;

    public MyListViewAdapter(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        newestDataLists = new ArrayList<>();
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        volleySingleton = VolleySingleton.getInstance();
    }

    public void addNewestDataLists(List<NewestData.DataEntity> newestDataLists) {
        this.newestDataLists = newestDataLists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return newestDataLists.size();
    }

    @Override
    public Object getItem(int position) {
        return newestDataLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyNewestViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_followings_item, parent, false);
            holder = new MyNewestViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (MyNewestViewHolder) convertView.getTag();
        bindNetWorkImageView(holder, position);
        bindAvaterNetWorkIV(holder, position);
        bindFollowsContent(holder, position);
        holder.followsName.setText(newestDataLists.get(position).getAuthor().getUsername());
        holder.followsDate.setText(newestDataLists.get(position).getCreated());
        holder.followsLikesCount.setText(newestDataLists.get(position).getLikes() + "");
        holder.followsCommentCounts.setText(newestDataLists.get(position).getComments() + "");
        return convertView;
    }

    private void bindFollowsContent(MyNewestViewHolder holder, int position) {
        if (newestDataLists.get(position).getContent().length() > 0) {
            holder.followsContent.setText(newestDataLists.get(position).getContent());
        }
        holder.followsContent.setVisibility(View.GONE);
    }

    //avater
    private void bindAvaterNetWorkIV(MyNewestViewHolder holder, int position) {
        holder.followsAvater.setImageUrl(newestDataLists.get(position).getAuthor().getAvatar(), imageLoader);
        holder.followsAvater.setErrorImageResId(R.mipmap.ic_launcher);
        holder.followsAvater.setDefaultImageResId(R.mipmap.ic_launcher);
    }

    //shared photo
    private void bindNetWorkImageView(final MyNewestViewHolder holder, int position) {
        if (newestDataLists.get(position).getPhoto().length() > 0) {
            holder.followsNI.setDefaultImageResId(R.mipmap.ic_launcher);
            holder.followsNI.setErrorImageResId(R.mipmap.ic_launcher);
            holder.followsNI.setImageUrl(newestDataLists.get(position).getPhoto(), imageLoader);
            ImageRequest imageRequest = new ImageRequest(newestDataLists.get(position).getPhoto(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    Palette.Builder builder = Palette.from(response);
                    builder.generate(new Palette.PaletteAsyncListener() {
                        @Override
                        public void onGenerated(Palette palette) {
                            Palette.Swatch virbant = palette.getVibrantSwatch();
                            if (virbant != null) {
                                holder.followsLayout.setBackgroundColor(virbant.getRgb());
                                holder.color = virbant.getRgb();
                            }
                        }
                    });
                }
            }, 40, 40, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            volleySingleton._addRequest(imageRequest);
        } else {
            holder.followsNI.setVisibility(View.GONE);
        }
    }

    class MyNewestViewHolder {
        RelativeLayout followsLayout;
        CardView followsCardView;
        NetworkImageView followsAvater;
        TextView followsName, followsDate, followsContent, followsLikesCount, followsCommentCounts;
        NetworkImageView followsNI;
        int color;

        public int getColor() {
            return color;
        }

        public MyNewestViewHolder(View itemView) {
            followsAvater = (NetworkImageView) itemView.findViewById(R.id.followsAvater);
            followsLayout = (RelativeLayout) itemView.findViewById(R.id.followsLayout);
            followsCardView = (CardView) itemView.findViewById(R.id.followsCardView);
            followsName = (TextView) itemView.findViewById(R.id.followsName);
            followsDate = (TextView) itemView.findViewById(R.id.followsDate);
            followsContent = (TextView) itemView.findViewById(R.id.followsContent);
            followsLikesCount = (TextView) itemView.findViewById(R.id.followsLikesCount);
            followsCommentCounts = (TextView) itemView.findViewById(R.id.followsCommentCounts);
            followsNI = (NetworkImageView) itemView.findViewById(R.id.followsNI);
            int color = 0;
        }


    }

}
